package com.studio49.exmple.controller.web.product;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.studio49.exmple.common.constant.ForwardConstant;
import com.studio49.exmple.common.constant.SysConstant;
import com.studio49.exmple.common.util.PubDateUtils;
import com.studio49.exmple.core.entity.pub.PageView;
import com.studio49.exmple.entity.web.product.Notice;
import com.studio49.exmple.mapper.web.product.NoticeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;

@Controller
@RequestMapping("/product/notice")
public class NoticeController {
	
	@Autowired
	private NoticeMapper noticeMapper;

	/**
	 * 列表页
	 * @return
	 */
	@RequestMapping("/list")
	public String list(){
		return ForwardConstant.PRODUCT_H + ForwardConstant.NOTICE + ForwardConstant.LIST;
	}
	
	/**
	 * 分页查询
	 * @param keyWords
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/findByPage")
	@ResponseBody
	public PageView findByPage(String keyWords, Integer pageNow, Integer pageSize){
		if(pageNow == null) {
			pageNow = 1;
		}
		PageHelper.startPage(pageNow, pageSize);
		Example example = new Example(Notice.class);
//		Criteria criteria = example.createCriteria();
//		if(StringUtils.isNotBlank(keyWords)) {
//			criteria.andLike("name", "%" + keyWords + "%");
//		}
		List<Notice> list = noticeMapper.selectByExample(example);

		PageView pageView = new PageView(list);
		return pageView;
	}

	/**
	 * 打开角色新增画面
	 *
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return ForwardConstant.PRODUCT_H + ForwardConstant.NOTICE + ForwardConstant.ADD;
	}

	/**
	 * 保存角色信息
	 *
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("addEntity")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(Notice notice) throws Exception {

		Date now = PubDateUtils.getNow();
		notice.setCreateTime(now);
		notice.setModifyTime(now);

		noticeMapper.insertSelective(notice);
		return SysConstant.SUCCESS;
	}

	/**
	 * 角色删除操作
	 *
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional
	public String deleteEntity(String ids) throws Exception {
		String[] ids_c = ids.split(",");

		for(String id : ids_c) {
			noticeMapper.deleteByPrimaryKey(id);
		}
		return SysConstant.SUCCESS;
	}

	/**
	 * 打开角色编辑画面
	 *
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("editUI")
	public String editUI(Long id, Model model, HttpServletRequest request) throws Exception {
		Notice notice = noticeMapper.selectByPrimaryKey(id);
		model.addAttribute("notice", notice);
		return ForwardConstant.PRODUCT_H + ForwardConstant.NOTICE + ForwardConstant.EDIT;
	}

	/**
	 * 角色编辑保存
	 *
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("editEntity")
	public String editEntity(Notice notice) throws Exception {
		Date now = PubDateUtils.getNow();
		notice.setModifyTime(now);
		noticeMapper.updateByPrimaryKeySelective(notice);
		return SysConstant.SUCCESS;
	}
	
}
